<?php
class Default_Model_Photo extends Zend_Db_Table{

	protected $_name = PHOTO_TABLE;
	protected $_primary = 'photo_id';

	private $PHOTO_ID = 'photo_id';
	private $TOUR_ID = 'tour_id';
	private $USER_ID = 'user_id';
	private $LOCATION_ID = 'location_id';

	protected $db;

	public function __construct(){
		//Goi ket noi voi Zend Db
		$this->db = Zend_Registry::get('connectDB');
	}

	protected $_messageError = null;
	
	public function countPhoto(){
		$select = $this->db->select()
		->from(PHOTO_TABLE,array('COUNT(photo_id) AS totalPhoto'));
		$result = $this->db->fetchOne($select);
		return $result;
	}
	
	public function listAllPhoto(){
		$select = $this->db->select()->from(PHOTO_TABLE);
		$select->order($this->PHOTO_ID.' DESC');
		return $select;
	}
	
	public function countMyIdlePhoto($userId){
		$select = $this->db->select()->from(PHOTO_TABLE,array('COUNT(photo_id) AS totalPhoto'));
		$select->where($this->USER_ID.' = ?',$userId)
		->where($this->TOUR_ID.' = ?',0)
		->where($this->LOCATION_ID.' = ?',0);
		$result = $this->db->fetchOne($select);
		return $result;
	}
	
	public function getMyIdlePhoto($userId){
		$select = $this->db->select()->from(PHOTO_TABLE)->where($this->USER_ID.' = ?',$userId)
														->where($this->TOUR_ID.' = ?',0)
														->where($this->LOCATION_ID.' = ?',0);
		$select->order($this->PHOTO_ID.' DESC');
		return $select;
	}

	public function getPhotoInfoById($photoId) {
		$select = $this->db->select()->from(PHOTO_TABLE)->where($this->PHOTO_ID.' = ?',$photoId);
		$result  = $this->db->fetchAll($select);
		return $result;
	}
	
	// get photo for tour
	public function getPhotoByTourId($tourId) {
		$select = $this->db->select()->from(PHOTO_TABLE)->where($this->TOUR_ID.' = ?',$tourId);
		$result  = $this->db->fetchAll($select);
		return $result;
	}
	
	// get photo for location
	public function getPhotoByLocId($locId) {
		$select = $this->db->select()->from(PHOTO_TABLE)->where($this->LOCATION_ID.' = ?',$locId);
		$result  = $this->db->fetchAll($select);
		return $result;
	}
	
	// get photo by user
	public function getPhotoByUserId($userId) {
		$select = $this->db->select()->from(PHOTO_TABLE)->where($this->USER_ID.' = ?',$userId);
		$result  = $this->db->fetchAll($select);
		return $result;
	}

	// update info
	public function updatePhotoInfo($arrParam = null, $photoId){
		try {
			$this->db->update(PHOTO_TABLE, $arrParam, $this->PHOTO_ID.' = '.$photoId);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}

	// Them user
	public function insertPhoto($arrParam = null){
		try {
			$this->db->insert(PHOTO_TABLE,$arrParam);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}

	// Xoa thong tin user
	public function deletePhotoById($photoId){
		try {
			$this->db->delete(PHOTO_TABLE,$this->PHOTO_ID.' = ' . $photoId);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}


	public function getError(){
		return $this->_messageError;
	}
}